package com.zhaogang.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.zhaogang.dao.UserAccountMapper;
import com.zhaogang.dto.AdminUserDto;
import com.zhaogang.entity.Page;
import com.zhaogang.entity.UserAccount;
import com.zhaogang.service.UserAccountService;

@Service
public class UserAccountServiceImpl implements UserAccountService {
	
	@Resource
	private UserAccountMapper userAccountMapper;

	
	
	/**
	 * 管理员查询用户信息
	 */
	public List<AdminUserDto> adminQueryUser(Page p) {
		// TODO Auto-generated method stub
		return userAccountMapper.adminQueryUser(p.getStartPos(), p.getPageSize());
	}

	/**
	 * 管理员删除用户信息
	 */
	public void deleteUser(String userAccount) {
		userAccountMapper.deleteUser(userAccount);
	}
	
	/**
	 * 恢复用户信息
	 */
	public void resumeUser(String userAccount){
		userAccountMapper.resumeUser(userAccount);
	}

	/**
	 * 查询全部用户分页
	 */
	public Page findPage(Page page) {
		int totalCount = userAccountMapper.queryCount();
		Integer pageNow;
		pageNow=page.getPageNow();
		Page p =new Page(totalCount, pageNow);
		p.setTotalCount(totalCount);
		return p;
	}

	
	
	/**
	 * 条件查询用户分页
	 */
	public Page findPage1(Page page,String userAccount,String realname) {
		int totalCount = userAccountMapper.queryCount1(userAccount,realname);
		System.out.println("totalCount"+totalCount);
		Integer pageNow;
		pageNow=page.getPageNow();
		Page p =new Page(totalCount, pageNow);
		p.setTotalCount(totalCount);
		return p;
	}
	/**
	 * 条件查询
	 */
	public List<AdminUserDto> adminQueryByCon(String userAccount,String realname, Page p) {
		System.out.println("start"+p.getStartPos());
		System.out.println("size"+p.getPageSize());
		return userAccountMapper.adminQueryByCon(userAccount,realname,p.getStartPos(), p.getPageSize());
	}
	
	public List<UserAccount> selectList() {
		// TODO Auto-generated method stub
		return userAccountMapper.selectList();
	}
	
	@Transactional
	public int registValidate(UserAccount userAccount) {
		List<UserAccount> userAccountList = userAccountMapper.selectList();
		for (UserAccount userAccount1 : userAccountList) {
			if(userAccount1.getUserAccount().equals(userAccount.getUserAccount())){
				return 0;
			}
		}
		return userAccountMapper.insertUserAccount(userAccount);
		
	}
	
	
	public String loginValidate(UserAccount userAccount) {
		List<UserAccount> userAccountList = userAccountMapper.selectList();
		String msg = "";
		String password = "";
		boolean b = false;
		
		for (UserAccount userAccount2 : userAccountList) {
			if(userAccount2.getUserAccount().equals(userAccount.getUserAccount())){
				password = userAccount2.getPassword();
				b = true;
				break;
			}
		}
		
		if(b){
			if(password.equals(userAccount.getPassword())){
				msg = "succcess";
			}else{
				msg = "您输入的密码不正确,请重新输入";
			}
		}else{
			msg = "您输入的账号不存在,请重新输入";
		}
		return msg;
	}
	
	public UserAccount loginVAlidatemd5(UserAccount userAccount) {
		List<UserAccount> userAccountList = userAccountMapper.selectList();
		
		for (UserAccount userAccount2 : userAccountList){
			if(userAccount2.getUserAccount().equals(userAccount.getUserAccount())){
		
				return  userAccount2;			
			}
		}	
		return null;
	}
			
	

}
